<?php

namespace app\api\controller;

use app\common\service\Payment;
use app\common\service\Redpack;
use app\common\service\Invite;
use app\common\library\AddressCode;

class Test extends \think\Controller
{


    public function __construct()
    {
        parent::__construct();

        $pwd = $this->request->param("pwd");

        if($pwd != '12345678'){
            $this->error("NO");
        }
    }
     

    public function red()
    {
        $ads = \app\common\model\ads\Ad::where('createtime','<',1589020435)
        ->where('type',1)
        ->select();

        foreach ($ads as $key => $ad) {
            $total = \app\common\model\ads\GoldLog::where('ad_id', $ad->id)->sum('gold_num');

            $ad->gold_num = $total;
            $ad->save();
        }
    }

    public function address($code)
    {
        $res = AddressCode::parse($code);

        var_dump($res);
    }

    public function inviteImg($id)
    {
        $user = \app\common\model\User::find($id);

        if (!$user) {
            return "用户不存在";
        }

        $res = Invite::createInviteImg($user, 1);

        var_dump($res);
    }


    public function area()
    {
        $url = 'https://apis.map.qq.com/ws/district/v1/list?key=ABHBZ-2R463-QP53S-3CI2A-Z56WT-VZB3W';

        $res = fhcurl($url);

        $res = json_decode($res, 1);

        if (!$res || !isset($res['result'])) {
            return 'failed';
        }

        $insertData = [];

        $model = new \app\common\model\TxArea();

        foreach ($res['result'] as $key => $list) {
            $level = $key + 1;
            foreach ($list as $k => $item) {
                if ($model->where('area_code', $item['id'])->find()) {
                    continue;
                }
                $loc = $item['location'];
                $insertData[] = [
                    'level'  => $level,
                    'name'   => $item['fullname'],
                    'area_code'   => $item['id'],
                    'lat'   => isset($loc['lat']) ? $loc['lat'] : 0,
                    'lng'   => isset($loc['lng']) ? $loc['lng'] : 0,
                    'pinyin' => isset($item['pinyin']) && is_array($item['pinyin']) ? implode(" ", $item['pinyin']) : '',
                ];
            }
        }

        $insert = $model->insertAll($insertData);

        return "共计导入{$insert}条";
    }

    public function update(){
        $ads = \app\common\model\ads\Ad::where('id','gt', 0)->select();

        foreach ($ads as $key => $value) {
            $gold = \app\common\model\ads\GoldLog::where('ad_id', $value->id)->sum('gold_num');
            if($gold){
                $value->gold_num = $gold;
                $value->save();
            }
        }
    }

    public function oss(){
       
        // $ads = \app\common\model\ads\Ad::where('category',1)->select();

        // foreach($ads as $k => $ad){
        //     $res = \turbo\Video::dealAd($ad['id']);
        //     var_dump($res);
        // }

        var_dump(parse_url('https://www.furunkeiji.com/uploads/20200504/90e6a7fa4f1acb62d9c508b4c42438e6.mp4'));

    }

    public function goldlog(){
        $logs = \app\common\model\ads\GoldLog::where('user_id','>', 0)->select();

        foreach ($logs as $key => $log) {
            $res = \app\common\model\huntermanager\Hunter::rewardForAdView($log);

            var_dump($res);
        }
    }
}
